package com.qymerge.manage.base.dao.impl;

import com.qymerge.manage.base.dao.IMetaCompanyDao;
import com.qymerge.manage.base.ro.MetaCompanyRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公司
 */
@Repository
public class MetaCompanyDaoImpl extends BaseDao implements IMetaCompanyDao {

    private String dwd_MCRS_MDM_meta_Company = "SELECT c.*,c2.Code as ParentCode, c2.Name as ParentName " +
            " FROM dwd_MCRS_MDM_meta_Company c left join dwd_MCRS_MDM_meta_Company c2 on c.ParentId = c2.Id" +
            " where 1=1 ";

    @Override
    public List list(MetaCompanyRO ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();

        String sql = dwd_MCRS_MDM_meta_Company;
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (StringUtils.isNotBlank(ro.getId())) {
            sql += " and c.Id=:Id";
            paramMap.put("Id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getCode())) {
            sql += " and c.Code like :Code";
            paramMap.put("Code", "%" + ro.getCode() + "%");
        }

        if (StringUtils.isNotBlank(ro.getName())) {
            sql += " and c.Name like :Name";
            paramMap.put("Name", "%" + ro.getName() + "%");
        }

        if (ro.getActived() > -1) {
            sql += " and c.Actived=:Actived";
            paramMap.put("Actived", ro.getActived());
        }

        // 分页数据获取
        sql += " order by Id desc limit :skip,:sizes";
        paramMap.put("skip", skip);
        paramMap.put("sizes", size);

        return getNamedParameterJdbcTemplate().query(sql, paramMap, new BeanPropertyRowMapper<MetaCompanyRO>(MetaCompanyRO.class));
    }

    @Override
    public int total(MetaCompanyRO ro) {
        String sql = "select count(1) from dwd_MCRS_MDM_meta_Company where 1=1";
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (StringUtils.isNotBlank(ro.getId())) {
            sql += " and Id=:Id";
            paramMap.put("Id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getCode())) {
            sql += " and Code like :Code";
            paramMap.put("Code", "%" + ro.getCode() + "%");
        }

        if (StringUtils.isNotBlank(ro.getName())) {
            sql += " and Name like :Name";
            paramMap.put("Name", "%" + ro.getName() + "%");
        }

        if (ro.getActived() > -1) {
            sql += " and Actived=:Actived";
            paramMap.put("Actived", ro.getActived());
        }

        return getNamedParameterJdbcTemplate().queryForObject(sql, paramMap, Integer.class);
    }

    @Override
    public MetaCompanyRO findOne(String id) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_MDM_meta_Company);
        sql.append("and c.Id=?");
        RowMapper<MetaCompanyRO> rowMapper = new BeanPropertyRowMapper<MetaCompanyRO>(MetaCompanyRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, id);
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public List<MetaCompanyRO> findAll(MetaCompanyRO ro) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_MDM_meta_Company);
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (StringUtils.isNotBlank(ro.getId())) {
            sql.append(" and c.Id=:Id");
            paramMap.put("Id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getCode())) {
            sql.append(" and c.Code like :Code");
            paramMap.put("Code", "%" + ro.getCode() + "%");
        }

        if (StringUtils.isNotBlank(ro.getName())) {
            sql.append(" and c.Name like :Name");
            paramMap.put("Name", "%" + ro.getName() + "%");
        }

        if (ro.getActived() > -1) {
            sql.append(" and c.Actived=:Actived");
            paramMap.put("Actived", ro.getActived());
        }

        sql.append(" order by Id desc");
        return getNamedParameterJdbcTemplate().query(sql.toString(), paramMap, new BeanPropertyRowMapper<MetaCompanyRO>(MetaCompanyRO.class));
    }

    @Override
    public MetaCompanyRO findOneByName(String companyName) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_MDM_meta_Company);
        sql.append("and c.Name=?");
        RowMapper<MetaCompanyRO> rowMapper = new BeanPropertyRowMapper<MetaCompanyRO>(MetaCompanyRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, companyName);
        } catch (DataAccessException e) {
            return null;
        }
    }


}
